// components/index/category-nav/category-nav.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // list: [
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/great_specials.png", name: "超值特价"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/non-staple_food.png", name: "副食"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/hotpot.png", name: "火锅套餐"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/seafood.png", name: "冻品生鲜"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/snacks.png", name: "休闲零食"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/fruit.png", name: "瓜果蔬菜"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/rice.png", name: "粮油米面"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/furniture.png", name: "家居百货"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/office_stationery.png", name: "办公文具"},
    //   {poster: "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/index_category/animal.png", name: "萌宠生活"}
    // ],
    slideWidth: '',//滑块宽
    slideLeft: 0 ,//滑块位置
    totalLength: '',//当前滚动列表总长
    slideShow: false,
    slideRatio: '',
    scrollViewLeft: 0
  },
  attached(){
    console.log(this.data.list.length)
    this.getDataInit();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    swipclick: function (e) {

      console.log(e.currentTarget.dataset.itemid);
    
      app.globalData.id_for_category = e.currentTarget.dataset.itemid;
    
      wx.switchTab({
    
        url: '/pages/catalog/catalog',
    
        success: function (e) {
    
          var page = getCurrentPages().pop();
    
                      if (page == undefined || page == null) return;
    
                      page.onLoad();
    
        }
    
      })
    
    },
    scrollSlide(e){
      // console.log(e.detail);
      let totalLength,
          slideRatio,
          slideWidth,
          slideLeft,
          slideShow = true;
      const { scrollLeft, scrollWidth } = e.detail;
      // 总长度 ： totalLength = scrollWidth + 702
      totalLength = scrollWidth + 702;
      // 比例： slideRatio = 64 / totalLength //分子小，分母大
      slideRatio = 64 / totalLength;
      // 滑块宽 slideRatio * 702
      slideWidth = slideRatio * (702 * 2) - 15;
      // 滑块位置 slideRatio * scrollLeft
      slideLeft = slideRatio * scrollLeft
      if(slideWidth >= totalLength){
        slideShow = false
      }
      this.setData({
        totalLength,
        slideRatio,
        slideWidth,
        slideLeft,
        slideShow
      })
    },
    getDataInit(){
      setTimeout(()=>{
        if(this.data.list.length > 0){
          this.setData({
            scrollViewLeft: 1
          })
        }else{
          this.getDataInit();
        }
      }, 500)
    }
  }
})
